﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    public class WorkSpaceItemPopupBar : Popup
    {
        public WorkSpaceItemPopupBar(WorkspaceTreeViewItem ownerItem)
        {
            this.MouseLeave += WorkSpaceItemPopupBar_MouseLeave;
            this.ownerItem = ownerItem;
            this.Placement = PlacementMode.Mouse;

            imgAddSameLevelItem = new Image() { Width = 16, Height = 16, };
            imgNewFolder = new Image() { Width = 16, Height = 16, };

            if (ownerItem != null && ownerItem.ItemType == WorkspaceTreeViewItem.Type.Folder)
            {
                imgAddSameLevelItem.Source = bmiNewSameLevelFolderIcon;
                btnAddSameLevelItem.ToolTip = "新建同级目录。";

                imgNewFolder.Source = bmiNewSubLevelFolderIcon;
                btnNewFolder.ToolTip = "新建下级目录。\r\n提示：目录比文件好用！";
            }
            else
            {
                imgAddSameLevelItem.Source = bmiNewDocumentIcon;
                btnAddSameLevelItem.ToolTip = "新建同级文件。";

                imgNewFolder.Source = bmiNewSameLevelFolderIcon;
                btnNewFolder.ToolTip = "新建同级目录。\r\n提示：目录比文件好用！";
            }

            btnAddSameLevelItem.Content = imgAddSameLevelItem;
            btnNewFolder.Content = imgNewFolder;

            StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };

            btnNewFolder.Style = FindResource("LSquareButtonStyle") as Style;
            btnNewFolder.BorderThickness = new Thickness(1);
            btnNewFolder.Click += BtnNewFolder_Click;
            sp.Children.Add(btnNewFolder);

            btnOpen.Style = FindResource("LSquareButtonStyle") as Style;
            btnOpen.BorderThickness = new Thickness(1);
            btnOpen.ToolTip = ((ownerItem == null || string.IsNullOrWhiteSpace(ownerItem.Title)) ? "打开文件。" : $"打开 {ownerItem.Title}。");
            btnOpen.Click += BtnOpen_Click;
            sp.Children.Add(btnOpen);

            btnAddLink.Style = FindResource("LSquareButtonStyle") as Style;
            btnAddLink.BorderThickness = new Thickness(1);
            btnAddLink.ToolTip = ((ownerItem == null || string.IsNullOrWhiteSpace(ownerItem.Title)) ? "作为链接插入到当前文档。" : $"将对 {ownerItem.Title} 的链接插入当前文档。");
            btnAddLink.Click += BtnAddLink_Click;
            sp.Children.Add(btnAddLink);

            btnAddSameLevelItem.Style = FindResource("LSquareButtonStyle") as Style;
            btnAddSameLevelItem.BorderThickness = new Thickness(1);
            btnAddSameLevelItem.Click += BtnAddSameLevelItem_Click;
            sp.Children.Add(btnAddSameLevelItem);

            btnPreviewWithRevealJs.Style = FindResource("LSquareButtonStyle") as Style;
            btnPreviewWithRevealJs.BorderThickness = new Thickness(1);
            btnPreviewWithRevealJs.Click += BtnPreviewWithRevealJs_Click;
            btnPreviewWithRevealJs.ToolTip = "调用 reveal.js 演示，Ctrl+Alt+P。";
            sp.Children.Add(btnPreviewWithRevealJs);

            btnCopyFilePath.Style = FindResource("LSquareButtonStyle") as Style;
            btnCopyFilePath.BorderThickness = new Thickness(1);
            btnCopyFilePath.Click += BtnCopyFilePath_Click; ;
            btnCopyFilePath.ToolTip = "复制此文件路径字符串。";
            sp.Children.Add(btnCopyFilePath);

            mainBorder = new Border()
            {
                Child = sp,
                BorderBrush = Brushes.Black,
                Padding = new Thickness(1),
                SnapsToDevicePixels = true,
                BorderThickness = new Thickness(1),
                Background = Brushes.WhiteSmoke,
            };

            RenderOptions.SetBitmapScalingMode(mainBorder, BitmapScalingMode.NearestNeighbor);
            this.Child = mainBorder;
        }

        private void BtnCopyFilePath_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Windows.Clipboard.SetDataObject(this.OwnerItem.FullPath);
            }
            catch (Exception ex)
            {
                LMessageBox.ShowWarning(ex.Message);
            }
        }

        private void BtnPreviewWithRevealJs_Click(object sender, RoutedEventArgs e)
        {
            Utils.RevealJsTools.Show();
        }

        private void BtnNewFolder_Click(object sender, RoutedEventArgs e)
        {
            if (ownerItem != null && ownerItem.ItemType == WorkspaceTreeViewItem.Type.Folder)
            {
                Globals.MainWindow.NewSubLevelFolder();
            }
            else
            {
                Globals.MainWindow.NewSameLevelFolder();
            }
        }

        private void WorkSpaceItemPopupBar_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.IsOpen = false;
        }

        private Border mainBorder;

        public Border MainBorder { get { return this.mainBorder; } }

        private void BtnAddSameLevelItem_Click(object sender, RoutedEventArgs e)
        {
            var selItem = Globals.MainWindow.tvWorkDirectory.SelectedItem as WorkspaceTreeViewItem;
            if (selItem == null)
            {
                LMessageBox.ShowWarning("请先选中一个文件或目录作为基准位置。");
                return;
            }

            switch (selItem.ItemType)
            {
                case WorkspaceTreeViewItem.Type.File:
                    {
                        Globals.MainWindow.NewSameLevelFile();
                        break;
                    }
                case WorkspaceTreeViewItem.Type.Folder:
                    {
                        Globals.MainWindow.NewSameLevelFolder();
                        break;
                    }
                default:
                    {
                        LMessageBox.ShowWarning("只能新建同级目录或同级文件。");
                        return;
                    }
            }
        }

        private void BtnAddLink_Click(object sender, RoutedEventArgs e)
        {
            Globals.MainWindow.InsertLinkToActiveDocument(this.ownerItem);
        }

        private WorkspaceTreeViewItem ownerItem = null;

        public WorkspaceTreeViewItem OwnerItem
        {
            get { return this.ownerItem; }
        }

        private void BtnOpen_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.ownerItem != null) this.ownerItem.OpenFile();
        }

        private Button btnNewFolder = new Button()
        {
            Padding = new Thickness(1),
            Margin = new Thickness(1),
            Height = 24,
            Width = 24,
        };

        private Button btnOpen = new Button()
        {
            Padding = new Thickness(1),
            Margin = new Thickness(1),
            Height = 24,
            Width = 24,
            Content = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/openHS.png")),
            },
        };

        internal Button BtnOpen { get { return btnOpen; } }

        private Button btnAddLink = new Button()
        {
            Padding = new Thickness(1),
            Margin = new Thickness(1),
            Height = 24,
            Width = 24,
            Content = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/insert-link-3.png")),
            },
        };

        private Image imgAddSameLevelItem;

        private Image imgNewFolder;

        private static BitmapImage bmiNewDocumentIcon = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/WorkspacePanelIcons/NewSameLevelFile20.png"));

        private static BitmapImage bmiNewSameLevelFolderIcon = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/WorkspacePanelIcons/NewSameLevelFolder20.png"));

        private static BitmapImage bmiNewSubLevelFolderIcon = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/WorkspacePanelIcons/NewSubLevelFolder20.png"));

        internal Button BtnAddLink { get { return btnAddLink; } }

        private Button btnAddSameLevelItem = new Button()
        {
            Padding = new Thickness(1),
            Margin = new Thickness(1),
            Height = 24,
            Width = 24,
        };

        private Button btnPreviewWithRevealJs = new Button()
        {
            Padding = new Thickness(1),
            Margin = new Thickness(1),
            Height = 24,
            Width = 24,
            Content = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/reveal-js.png")),
            },
        };

        private Button btnCopyFilePath = new Button()
        {
            Padding = new Thickness(1),
            Margin = new Thickness(1),
            Height = 24,
            Width = 24,
            Content = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/edit-copy-3.png")),
            },
        };

        internal Button BtnAddSameLevelItem { get { return btnAddSameLevelItem; } }
    }
}
